import {message} from 'antd';
import {formatMessage} from './intl';
import {paymentType} from '../Enum';

export const newOrUpdateOrderValidator = (data, details) => {
    //增加判断必填字段
    const emptyErrors = [];
    if(!data || !data.brandId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brandId',
            defaultMessage: '品牌'
        }));
    if(!data || !data.dealerId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.dealerId',
            defaultMessage: '总代'
        }));
    if(!data || !data.saleOrderId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.salesOrderId',
            defaultMessage: '销售订单编号'
        }));
    if(data.payment === paymentType['L/C'] && !data.letterOfCreditId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.letterOfCreditId',
            defaultMessage: '信用证号'
        }));
    if((!data || !data.batchNumber))
        emptyErrors.push(formatMessage({
            id: 'validator.empty.batchNumber',
            defaultMessage: '批次号'
        }));
    if((!data || !data.transportType))
        emptyErrors.push(formatMessage({
            id: 'validator.empty.transportType',
            defaultMessage: '运输方式'
        }));
    if((!data || !data.shippingPortId))
        emptyErrors.push(formatMessage({
            id: 'validator.empty.shippingPortId',
            defaultMessage: '发运港口'
        }));
    if((!data || !data.destinationPort))
        emptyErrors.push(formatMessage({
            id: 'validator.empty.destinationPort',
            defaultMessage: '目的港口'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    if(details.length <= 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.detailRequired',
            defaultMessage: '清单不能为空'
        })}`);
        return false;
    }
    if(details.some(d => !d.price)) {
        message.warning(formatMessage({
            id: 'validator.message.price',
            defaultMessage: '单价必填'
        }));
        return false;
    }
    return true;
};

export const approveValidator = data => {
    //增加判断必填字段
    const emptyErrors = [];
    if(!data || !data.approveComment)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.approveComment',
            defaultMessage: '审核意见'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    return true;
};
